{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "66f7224b",
   "metadata": {},
   "source": [
    "* 2022/10/11 /week08\n",
    "* Python 课堂笔记"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ef8d215",
   "metadata": {},
   "source": [
    "## 本周内容\n",
    "> 1.代码的注释      \n",
    "> 2.用户输入       \n",
    "> 3.格式化输出"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f6bc9e58",
   "metadata": {},
   "source": [
    "### 1. 代码的注释"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98e8e034",
   "metadata": {},
   "source": [
    "> 1.一般写在代码块的最上边，或者每一行代码的最后，用于代码思路的记录。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9ab9e8fb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Author:VV"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "702af88a",
   "metadata": {},
   "source": [
    "> 2. ''注释内容 ''' ，一般是注释代码块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0d56111e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "三个引号注释代码块内容，不运行\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "import random\n",
    "wait_time = random.randint(1,60)\n",
    "print(wait_time)\n",
    "\n",
    "word = \"bottles\"\n",
    "print(word)\n",
    "'''\n",
    "print(\"三个引号注释代码块内容，不运行\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7caa14a",
   "metadata": {},
   "source": [
    "### 2. 用户输入"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7808d30e",
   "metadata": {},
   "source": [
    "> 1.input()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a46d151c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:akalzw\n",
      "密码:0901\n",
      "akalzw\n",
      "0901\n"
     ]
    }
   ],
   "source": [
    "# 登陆系统\n",
    "username = input(\"账号:\")\n",
    "password = input(\"密码:\")\n",
    "\n",
    "print(username)\n",
    "print(password)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "987b4102",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:akalzw\n",
      "密码:0901\n",
      "欢迎akalzw登录！\n"
     ]
    }
   ],
   "source": [
    "# 登陆系统 迭代1 ：增加了判断功能\n",
    "# 假设我们数据库有用户信息：账号：akalzw，密码: 0901\n",
    "user1 = \"akalzw\"\n",
    "password1 = \"0901\"\n",
    "\n",
    "username = input(\"账号:\")\n",
    "password = input(\"密码:\")\n",
    "\n",
    "# 情景一：输入信息正确，登陆成功\n",
    "if user1 == username and password1 == password:\n",
    "    print(\"欢迎\"+user1+\"登录！\")\n",
    "# 情景二：账号不存在或密码不正确\n",
    "else:\n",
    "    print(\"账号不存在或密码不正确!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b00669a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:akalzw\n",
      "密码:0901\n",
      "欢迎akalzw登录！\n"
     ]
    }
   ],
   "source": [
    "# 登陆系统 迭代2 ：增加循环功能\n",
    "# 假设我们数据库有用户信息：账号：admin，密码，123456\n",
    "user1 = \"akalzw\"\n",
    "password1 = \"0901\"\n",
    "\n",
    "for i in range(3,0,-1):  # i 是一个 int\n",
    "    username = input(\"账号:\")\n",
    "    password = input(\"密码:\")\n",
    "\n",
    "    # 情景一：输入信息正确，登陆成功\n",
    "    if user1 == username and password1 == password:\n",
    "        print(\"欢迎\"+user1+\"登录！\")\n",
    "        break # 终止循环\n",
    "    # 情景二：账号不存在或密码不正确\n",
    "    else:\n",
    "        if i == 1:\n",
    "            print(\"三次输入失败，请1小时后再来尝试\")\n",
    "        else:\n",
    "            print(\"账号不存在或密码不正确!\")\n",
    "            print(\"您还有\"+str(i-1)+\"次尝试的机会\") # str强制将int转换成str\n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb9b99b2",
   "metadata": {},
   "source": [
    "### 3. 格式化输出"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79fbd19a",
   "metadata": {},
   "source": [
    "> 1.字符串拼接            \n",
    "> 2.占位符               \n",
    "> 3.format()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "da00a42c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名：akalzw\n",
      "年龄：18\n",
      "工作：IT\n",
      "收入：99999\n",
      "地区：NYC\n",
      "-----INFO OF akalzw------\n",
      "年龄：18\n",
      "工作：IT\n",
      "收入：99999\n",
      "地区：NYC\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法一：字符串拼接\n",
    "name = input(\"姓名：\")\n",
    "age = input(\"年龄：\")\n",
    "job = input(\"工作：\")\n",
    "salary = input(\"收入：\")\n",
    "reg = input(\"地区：\")\n",
    "\n",
    "info = '''-----INFO OF '''+ name +'''------'''+'''\n",
    "年龄：'''+age+'''\n",
    "工作：'''+job+'''\n",
    "收入：'''+salary+'''\n",
    "地区：'''+reg\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "74beafa0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "年龄：18\n",
      "18 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "# input 作为用户输入，其都以字符串形式返回\n",
    "age = input(\"年龄：\")\n",
    "print(age,type(age))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "631dde75",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名：AKALZW\n",
      "年龄：19\n",
      "工作：IT\n",
      "收入：9999999\n",
      "地区：NYC\n",
      "-----INFO OF AKALZW------\n",
      "姓名：AKALZW\n",
      "年龄：19\n",
      "工作：IT\n",
      "收入：9999999\n",
      "地区：NYC\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法二：占位符 %s str, 依次有序输入\n",
    "name = input(\"姓名：\")\n",
    "age = int(input(\"年龄：\")) # 强制将 str 转换成 int\n",
    "job = input(\"工作：\")\n",
    "salary = input(\"收入：\")\n",
    "reg = input(\"地区：\")\n",
    "\n",
    "info = '''-----INFO OF %s------\n",
    "姓名：%s\n",
    "年龄：%d\n",
    "工作：%s\n",
    "收入：%s\n",
    "地区：%s'''%(name,name,age,job,salary,reg)\n",
    "print(info)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "feacc225",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名：AKALZW\n",
      "年龄：19\n",
      "工作：IT\n",
      "收入：99999999\n",
      "地区：NYC\n"
     ]
    }
   ],
   "source": [
    "# 用户注册系统 方法三：format() 格式化输出函数，参数传递，不需要考虑位置\n",
    "name = input(\"姓名：\")\n",
    "age = int(input(\"年龄：\")) # 强制将 str 转换成 int\n",
    "job = input(\"工作：\")\n",
    "salary = input(\"收入：\")\n",
    "reg = input(\"地区：\")\n",
    "\n",
    "info = '''-----INFO OF {姓名}------\n",
    "姓名：{姓名}\n",
    "年龄：{年龄}\n",
    "工作：{工作}\n",
    "收入：{收入}\n",
    "地区：{地区}'''.format(年龄 = age,姓名 = name,工作 = job,收入 = salary, 地区 = reg)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "350668eb",
   "metadata": {},
   "source": [
    "## 课后练习\n",
    "* 要求：\n",
    "> 1.运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。           \n",
    "> 2.让用户输入商品编号进行商品的购买。                 \n",
    "> 3.用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。            \n",
    "> 4.用户可以随时退出购买，退出时打印用户已购买的商品和支付宝余额"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "01d4c6dc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的支付宝余额：400\n",
      "序号1代表：python，价格为100.00元\n",
      "序号2代表：English book，价格为60.00元\n",
      "序号3代表：exercise book，价格为40.00元\n",
      "序号4代表：pen，价格为10.00元\n",
      "请输入需要加入购物车的商品序号：1\n",
      "请输入需要购买商品1的数量：2\n",
      "是否继续添加商品至购物车？y/n：n\n",
      "结束购物车添加，进入结算环节\n",
      "您的余额为：200元\n"
     ]
    }
   ],
   "source": [
    "balance = int(input(\"请输入您的支付宝余额：\"))\n",
    "\n",
    "goods=[ {\"name\":\"python\",\"price\":100},\n",
    "         {\"name\":\"English book\",\"price\":60},\n",
    "         {\"name\":\"exercise book\",\"price\":40},\n",
    "         {\"name\":\"pen\",\"price\":10}\n",
    "]\n",
    "carInfo = {} #存储购买数量和商品价格\n",
    "shooping_cart = [] #追加购物车的内容至列表便于计算\n",
    "def goodsInfo(): #显示商品信息\n",
    "    for i in goods: #遍历goods列表中的每一个元素\n",
    "        Num = goods.index(i) + 1 #将元素下标值+1，定义商品序号\n",
    "        print('序号%d代表：%s，价格为%.2f元' % (Num,i['name'], i['price']))#由于元素都是键值对，所以可用i[]直接取值\n",
    " \n",
    "def shooping_cart(): #购物车\n",
    "    while True:\n",
    "        num = int(input('请输入需要加入购物车的商品序号：'))\n",
    "        if 0<num<=len(goods): \n",
    "            multiple = int(input('请输入需要购买商品%d的数量：' % num))\n",
    "            if multiple>0:\n",
    "                carInfo[multiple]=goods[num-1]['price']  #将输入的购买数量和商品价格作为键值对存入字典中\n",
    "            else:\n",
    "                print('您输入的商品数量有误，请重新输入')\n",
    "            ask = input('是否继续添加商品至购物车？y/n：')\n",
    "            if ask == 'y':\n",
    "                continue\n",
    "            else:\n",
    "                print('结束购物车添加，进入结算环节')\n",
    "                break\n",
    "    shooping_cart.append(carInfo)#将所有字典写入列表Car中，存储购物车序号及购买数量\n",
    " \n",
    "def settlement():#结算\n",
    "    balance = 0\n",
    "    num = list(carInfo.keys())  # 将所有序号的值赋给列表num\n",
    "    price = list(carInfo.values())  # 将所有数量的值赋给列表price\n",
    "    for i in range(len(num)):\n",
    "        total = balance + num[i] * price[i]\n",
    "    print('您的余额为：%d元'%total)\n",
    "        \n",
    " \n",
    "goodsInfo()\n",
    "shopCar()\n",
    "settlement()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "66306062",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
